package com.ldzy.goods.service;

import com.ldzy.common.pojo.Goods;
import com.ldzy.goods.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Transactional
    public boolean updateCountById(Goods goods){
        //1、先查询余额
        Goods dbGoods = goodsMapper.queryById(goods.getId());
        int x=0;
        if(dbGoods.getCount()<goods.getCount()) //库存不够
        {
            System.out.println("库存不够！");
            x=1/0;
        }
        System.out.println(dbGoods.getCount());
        goods.setCount(dbGoods.getCount()-goods.getCount());//新的库存
        //2、再更新
        if(goodsMapper.updateCountById(goods)>0)
            return true;
        else return false;
    }
}
